package itstudio.instructor.ui;

import itstudio.app.wq.R;
import itstudio.instructor.adapter.UserListAdapter;
import itstudio.instructor.config.Config;
import itstudio.instructor.config.ImageOptionsUtil;
import itstudio.instructor.entity.User;
import itstudio.instructor.http.TwitterRestClient;
import itstudio.instructor.util.LoginUtil;
import itstudio.instructor.xlistview.XListView;
import itstudio.instructor.xlistview.XListView.IXListViewListener;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Header;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nhaarman.listviewanimations.swinginadapters.AnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

public class MyFollowActivity extends Activity implements IXListViewListener {
	private XListView xListView;
	private UserListAdapter adapter;
	private ArrayList<User> userList;
	private View myfollow_back_view;
    private LinearLayout loadLayout;
    private LinearLayout loadNodataLayout;
	DisplayImageOptions options;
	private int page =0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_follow);
		options = ImageOptionsUtil.headImageOptions();
		findById();
		initView();
		setOnclick();

	}


	private void setOnclick() {
		// TODO Auto-generated method stub
		MyOnClick mListener = new MyOnClick();
		myfollow_back_view.setOnClickListener(mListener);

	}

	class MyOnClick implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.my_follow_back_layout:
				finish();
				break;

			default:
				break;
			}

		}

	}

	private void findById() {
		// TODO Auto-generated method stub
		xListView = (XListView) findViewById(R.id.myfollow_listview);
		myfollow_back_view = findViewById(R.id.my_follow_back_layout);
		loadLayout = (LinearLayout) findViewById(R.id.view_loading);
		loadNodataLayout = (LinearLayout)findViewById(R.id.view_load_nodata);

	}

	private void initView() {
		// TODO Auto-generated method stub
		// 填充listview
		userList = new ArrayList<User>();
		// 加载延迟
		adapter = new UserListAdapter(MyFollowActivity.this,
				userList, options);
		AnimationAdapter animAdapter = new SwingBottomInAnimationAdapter(
				adapter);
		xListView.setPullLoadEnable(true);
		xListView.setPullRefreshEnable(false);// 取消下拉刷新
		animAdapter.setAbsListView(xListView);
		animAdapter.setInitialDelayMillis(300);
		xListView.setAdapter(animAdapter);
		xListView.setXListViewListener(this);
		xListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				Intent intent = new Intent();
				TextView instructor_name=(TextView) arg1.findViewById(R.id.instructor_name);
				instructor_name.setTextColor(getResources().getColor(R.color.light_black));
				//进行判断如果是老师就显示老师个人信息  如果是学生就显示学生个人信息
				intent.setClass(MyFollowActivity.this, UserDetailActivity.class);
				User user = (User) adapter.getItem(position-1); // -1
				intent.putExtra("user", user);
				startActivity(intent);

			}
		});
		loadFollwing(1,true);

	}
	//加载辅导员
	private void loadFollwing(int page_num,final boolean refesh){
		RequestParams params = new RequestParams();
		params.put("page", page_num);
		TwitterRestClient.post(Config.AC_USER_FOLLOW, params, new AsyncHttpResponseHandler(){
			
			@Override
			public void onStart() {
				super.onStart();

			}
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				// TODO Auto-generated method stub
				onLoad();
				loadLayout.setVisibility(View.GONE);
				loadNodataLayout.setVisibility(View.GONE);
			}

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] data) {
				//加载成功
				onLoad();
				loadLayout.setVisibility(View.GONE);
				loadNodataLayout.setVisibility(View.GONE);
				Gson gson = new GsonBuilder()  
				  .setDateFormat("yyyy-MM-dd HH:mm:ss")  
				  .create();
				String response = new String(data);
				if(response.indexOf(Config.LOGIN)!=-1 && response.length()<12){
					LoginUtil.Login(MyFollowActivity.this);
				}
				// 有数据
				else {
					userList  = gson.fromJson( new String(data), new TypeToken<List<User>>(){}.getType());
					page = adapter.appendData(userList,refesh);
					
					if(page==0){
						loadNodataLayout.setVisibility(View.VISIBLE);
					}
					if(userList!=null  && userList.size()==10){
						xListView.setPullLoadEnable(true);
					}
					else{
						xListView.setPullLoadEnable(false);
						
					}
				}
				
			}});
	}
	// 设置下拉刷新时间
	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub

	}

	// 设置loadmore刷新时间
	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		loadFollwing(page+1,false);

	}

	private void onLoad() {
		xListView.stopRefresh();
		xListView.stopLoadMore();
	}

}
